/*============================================================
* AUTHOR: Van Nicoghosian
*
* DATE: March 29, 2012
*
* The purpose of this class is to provide a tree view of the
* task hierarchy.
*============================================================*/
package SOEN_Phase_3;

import java.util.ArrayList;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

@SuppressWarnings("serial")
public class TreeView  extends JPanel implements TreeSelectionListener
{

    private JTree tree;
    private Task[] tasks;
    private JFrame frame;
    private DefaultMutableTreeNode top;


    // MATT: Modified to avoid new object creation
	public TreeView(Object[] x)
	{
		super(new GridLayout(1,0));
    	tasks = new Task[x.length];

    	for (int i=0; i<x.length ;i++){
    		tasks[i] = (Task) x[i];
    	}
    	
        //Create and set up the window.
        frame = new JFrame("Tree View");  
    	
    	createTree();
        setShow(false);
	}
	
	private void createTree()
	{
        //Create the nodes.
        top = new DefaultMutableTreeNode("Tasks");
        createNodes(top);

        //Create a tree
        tree = new JTree(top);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

        //Listener
        tree.addTreeSelectionListener(this);

        //Create the scroll pane and add the tree to it.
        JScrollPane treeView = new JScrollPane(tree);
        Dimension minimumSize = new Dimension(100, 50);
        treeView.setMinimumSize(minimumSize);
        treeView.setPreferredSize(new Dimension(500, 300));

        //Add to this panel.
        frame.add(treeView);

        //Display the window.
        frame.pack(); 
	}

	// Required by TreeSelectionListener
    public void valueChanged(TreeSelectionEvent e) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();

        if (node == null) return;
    }

    // Create Tree View node hierarchy
    private void createNodes(DefaultMutableTreeNode top) {
        DefaultMutableTreeNode parent = null;

        // FOREACH Task
        for (Task task: tasks)
        {
            // IF Task is a top-level parent Task
        	if (task.isSubtask() == false)
        	{
                // Create top-level tree node
                parent = new DefaultMutableTreeNode(task.getTitle());

                // Recursively generate the entire tree branch, starting with this parent
                createBranch(parent, task);

                // Add the branch to the tree
                top.add(parent);
        	}
        }
    }

    // Private helper: recursively create a tree branch
    private void createBranch(DefaultMutableTreeNode parentNode, Task parentTask)
    {
        /*-----------------------------------------------------------
         * FOR each child, add a new node, and recurse if that child
         * has children of its own
         *-----------------------------------------------------------*/
        ArrayList<Task> subtasks = parentTask.getSubtasks();

        DefaultMutableTreeNode childNode;

        Task childTask;

        for(int i = 0; i != subtasks.size(); ++i)
        {
            // Get child task
            childTask = subtasks.get(i);

            // Create child node
            childNode = new DefaultMutableTreeNode(childTask.getTitle() );

            // Add child node
            parentNode.add( childNode );

            // IF child has children, recurse
            if( childTask.getSubtasks().size() > 0 )
            {
                createBranch(childNode, childTask);
            }
        }

    }
    
    // sets frame visibility
    public void setShow(boolean value)
    {
    	frame.setVisible(value);
    }
    
    public void treeUpdate(Object[] x)
    {
    	tasks = new Task[x.length];
    	
    	for (int i=0; i<x.length ;i++){
    		tasks[i] = (Task) x[i];
    	}
    	Rectangle bounds = frame.getBounds();
    	frame.dispose();
    	frame = new JFrame("Tree View");  
    	createTree();
    	frame.setBounds(bounds);
    }
}
